package org.tloss.games.mummy;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;

public class MainMenuScreen implements Screen {
	final MummyGame game;

	OrthographicCamera camera;
	SpriteBatch batch;
	Vector3 touchPoint;
	Rectangle playButton;
	Rectangle exitButton;

	public MainMenuScreen(final MummyGame game) {
		this.game = game;
		
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		camera = new OrthographicCamera(w, h);
		camera.position.set(w / 2, h / 2, 0);
		batch =  new SpriteBatch();
		touchPoint = new Vector3();
		float x = (w - Assets.BUTTON_WIDTH) / 2;
		float y = Assets.BUTTON_HEIGHT + 8;
		playButton = new Rectangle(x, y, Assets.BUTTON_WIDTH,
				Assets.BUTTON_HEIGHT);
		exitButton = new Rectangle(x, 0, Assets.BUTTON_WIDTH,
				Assets.BUTTON_HEIGHT);
	}

	public boolean update(float deltaTime) {
		if (Gdx.input.justTouched()) {
			camera.unproject(touchPoint.set(Gdx.input.getX(),
					Gdx.input.getY(), 0));
			if (OverlapTester.pointInRectangle(playButton, touchPoint.x,
					touchPoint.y)) {
				game.setScreen(new MapScreen(game));
				return false;
			}
		}
		return true;
	}

	public void draw(float deltaTime) {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		camera.update();
		batch.setProjectionMatrix(camera.combined);
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				batch.enableBlending();
				batch.begin();
				batch.draw(Assets.floor, i * 64, j * 64);
				batch.end();
			}
		}
		touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0);
		if (OverlapTester.pointInRectangle(playButton, touchPoint.x,
				touchPoint.y)) {
			Assets.font.setColor(Color.LIGHT_GRAY);
		} else {
			Assets.font.setColor(Color.WHITE);
		}
		Utils.drawButton(batch, Assets.font, "PLAY", playButton.x, playButton.y);
		if (OverlapTester.pointInRectangle(exitButton, touchPoint.x,
				touchPoint.y)) {
			Assets.font.setColor(Color.LIGHT_GRAY);
		} else {
			Assets.font.setColor(Color.WHITE);
		}
		Utils.drawButton(batch, Assets.font, "EXIT", exitButton.x, exitButton.y);

	}

	@Override
	public void render(float delta) {
		if(update(delta))
			draw(delta);
	}

	@Override
	public void resize(int width, int height) {

	}

	@Override
	public void show() {

	}

	@Override
	public void hide() {

	}

	@Override
	public void pause() {

	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {
		batch.dispose();
	}

}
